<!-- file mandor -->
<script> 
	
	$(function(){
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
		    if (this.id && this.id !== '') {
		        if (ids[this.id]) {
		            $(this).remove();
		        } else {
		            ids[this.id] = this
		        }
		    }
		});
		
		/* external function */
		
        function format(state) {
			tambahan = "";
			if (typeof $(state.element[0]).attr("alt") != "undefined")
				tambahan = $(state.element[0]).attr("alt");
            return state.text+tambahan ; 
        }
		
		$.fn.getCombobox = function(data, key, label, desc){
			var field = $(this);
			field.html("<option></option>");
			$.ajax({
				url : "c_public_function",
				data : data,
				type : 'POST', dataType : 'json'
			}).done(function(response){
				for(i=0;i<response.length;i++){
					var option = "<option value='"+response[i][key]+"' ";
					if (typeof response[i][desc] != "undefined")
						option += " alt='<div class=desc>"+response[i][desc]+"</div>'";
						
					option += " >"+response[i][label]+"</option>";
					field.append(option);
				}
				
				field.select2({
					placeholder: "-",
					allowClear: true,
					formatResult: format,
					formatSelection: format,
					escapeMarkup: function (m) {
						return m;
					}
				});
			});
		}
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_mandor = $("#task-mandor");
		grid_mandor = $("#grid-mandor");
		btnadd_mandor = $("#btn-add-mandor");
		btncancel_mandor =  $("#btn-cancel-mandor");
		btnsave_mandor = $("#btn-save-mandor");
		form_mandor = $("#form-mandor");
		portlet_mandor = $("#portlet-mandor");
		
		field_mandor_id = $("#mandor_id");
		field_mandor_nama = $("#mandor_nama");
		field_mandor_kode = $("#mandor_kode");
		field_mandor_bu = $("#mandor_bu");
		field_mandor_alamat = $("#mandor_alamat");
		field_mandor_kota = $("#mandor_kota");
		field_mandor_npwp = $("#mandor_npwp");
		field_mandor_telpon = $("#mandor_telpon");
		field_mandor_fax = $("#mandor_fax");
		field_mandor_email = $("#mandor_email");
		field_mandor_pengalaman = $("#mandor_pengalaman");
		field_mandor_kemampuan = $("#mandor_kemampuan");
		field_mandor_wakil = $("#mandor_wakil");
		field_mandor_tukang1 = $("#mandor_tukang1");
		field_mandor_tukang2 = $("#mandor_tukang2");
		field_mandor_pekerja = $("#mandor_pekerja");
		field_m_keuangan_id = $("#m_keuangan_id");
		field_m_pendanaan_id = $("#m_pendanaan_id");
		
		/* function */
		
		/* method started */
		
		/* make component */
		field_mandor_kemampuan.getCombobox({task : 'pekerjaan'},"pekerjaan_id","pekerjaan_ket");
		field_m_keuangan_id.getCombobox({task : 'keuangan'},"keuangan_id","keuangan_ket");
		field_m_pendanaan_id.getCombobox({task : 'pendanaan'},"pendanaan_id","pendanaan_ket");
        
		/* maybe you need * /
		
		field_task_assign_date.datepicker({ dateFormat : 'dd-MM-yy'});
		field_status.select2({
					placeholder: "-",
					allowClear: true,
					formatResult: format,
					formatSelection: format,
					escapeMarkup: function (m) {
						return m;
					}
				});
		m_staff_id.select2("val",dataRow.m_staff_id);
				
		/*                  */
		
		grid_mandor.unugrid({
			url: 'c_mandor',
			data: {task : 'LIST'},
			column :[
				{ label: 'Nama Mandor'  ,index: 'mandor_nama'  },
				{ label: 'Kode'  ,index: 'mandor_kode'  },
//				{ label: 'mandor_bu'  ,index: 'mandor_bu'  },
//				{ label: 'mandor_alamat'  ,index: 'mandor_alamat'  },
				{ label: 'NPWP'  ,index: 'mandor_npwp'  },
				{ label: 'Telepon'  ,index: 'mandor_telpon'  },
//				{ label: 'mandor_fax'  ,index: 'mandor_fax'  },
//				{ label: 'mandor_email'  ,index: 'mandor_email'  },
//				{ label: 'mandor_pengalaman'  ,index: 'mandor_pengalaman'  },
//				{ label: 'mandor_kemampuan'  ,index: 'mandor_kemampuan'  },
//				{ label: 'mandor_wakil'  ,index: 'mandor_wakil'  },
//				{ label: 'mandor_tukang1'  ,index: 'mandor_tukang1'  },
//				{ label: 'mandor_tukang2'  ,index: 'mandor_tukang2'  },
//				{ label: 'mandor_pekerja'  ,index: 'mandor_pekerja'  },
//				{ label: 'm_keuangan_id'  ,index: 'm_keuangan_id'  },
//				{ label: 'm_pendanaan_id'  ,index: 'm_pendanaan_id'  },
				
				{ label: ''  ,index: 'mandor_id',type: 'action' }
			],
			deleteClick : function(id,dataRow, c ){
				if (id != null){
					confr = confirm("Apa Anda yakin menghapus data ini?");
					if (confr){
						var data = $(this).data();
						$.ajax({
							type: 'POST',
							url: 'c_mandor',
							data: {mandor_id: id, task : 'DELETE'},
							success: function(response){
								if(response==1){
									grid_mandor.unugrid('refresh');
								}
							}
						});
					}
				}
			},
			editClick : function(id,dataRow, c ){
				if (id != null){
					task_mandor.val("UPDATE");
					
					field_mandor_id.val(dataRow.mandor_id);
					field_mandor_nama.val(dataRow.mandor_nama);
					field_mandor_kode.val(dataRow.mandor_kode);
					field_mandor_bu.val(dataRow.mandor_bu);
					field_mandor_alamat.val(dataRow.mandor_alamat);
					field_mandor_kota.val(dataRow.mandor_kota);
					field_mandor_npwp.val(dataRow.mandor_npwp);
					field_mandor_telpon.val(dataRow.mandor_telpon);
					field_mandor_fax.val(dataRow.mandor_fax);
					field_mandor_email.val(dataRow.mandor_email);
					field_mandor_pengalaman.val(dataRow.mandor_pengalaman);
					field_mandor_kemampuan.select2("val",eval('['+dataRow.mandor_kemampuan+']'));
					field_mandor_wakil.val(dataRow.mandor_wakil);
					field_mandor_tukang1.val(dataRow.mandor_tukang1);
					field_mandor_tukang2.val(dataRow.mandor_tukang2);
					field_mandor_pekerja.val(dataRow.mandor_pekerja);
					field_m_keuangan_id.select2("val",dataRow.m_keuangan_id);
					field_m_pendanaan_id.select2("val",dataRow.m_pendanaan_id);
					
                    $("input[name=mandor_status]").removeAttr("checked");
                    $("input[name=mandor_status][value='"+dataRow.mandor_status+"']").attr("checked","checked");
                    
                    
                    if (dataRow.mandor_status == "Badan Hukum")
                        field_mandor_bu.parent().parent().parent().show("slow");
                    else{
                        field_mandor_bu.parent().parent().parent().hide("slow");
                        field_mandor_bu.val("");
                    }
                    
                    $.ajax({
                        url : "c_mandor",
                        data : {task : "DETAIL", mandor_id : dataRow.mandor_id},
                        dataType : 'json',
                        type : 'post'
                    }).done(function(response){
                        $("#detail_wakil").html("");
                        for(i = 0; i < response.length; i++){
                            $("#detail_wakil").append('<div class="input-group wakil"><a  class="btn default" >'+(i + 1)+'. </a><input name="wakil_nama" class="form-control input-small " value="'+response[i].wakil_nama+'" /><a  class="btn default" >HP / No Telp</a><input name="wakil_telpon" class="form-control input-small" value="'+response[i].wakil_telpon+'"  /></div>');
                        }
                    });
					portlet_mandor.show();
				}
			}
		});
		
		/* event */
        
		$(".search input").keypress(function(e){
            if (e.keyCode == 13){
                $(".search button").click();
            }
        });
        
		$(".search button").click(function(){
            inputsearch = $(".search input").val();
            grid_mandor.unugrid("setData",{ search : inputsearch});
            grid_mandor.unugrid("refresh");
        });
        
        $("input[name=mandor_status]").click(function(){
            if ($(this).val() == "Badan Hukum")
                field_mandor_bu.parent().parent().parent().show("slow");
            else{
                field_mandor_bu.parent().parent().parent().hide("slow");
                field_mandor_bu.val("");
            }
        });
        
		btnadd_mandor.click(function(){
			btncancel_mandor.click();
			task_mandor.val("INSERT");
			portlet_mandor.show();
            
            field_mandor_wakil.val(0);
            field_mandor_wakil.keyup();
            field_m_keuangan_id.select2("val",0);
            field_m_pendanaan_id.select2("val",0);
            field_mandor_kemampuan.select2("val",0);
		});
		
		btncancel_mandor.click(function(){
			portlet_mandor.hide();
		});
		
		btnsave_mandor.click(function(){
            
			data = form_mandor.serializeObject();
			$.ajax({
				url : "c_mandor",
				data : data,
				type : 'POST'
			}).done(function(response){
				grid_mandor.unugrid('refresh');
				btncancel_mandor.click();
			});
		});
        
        field_mandor_wakil.keyup(function(){
            jml = parseInt(field_mandor_wakil.val());
            current = $("#detail_wakil").find(".wakil").length;
            tambah = jml - current;
            if (tambah >= 0){
                for(i = current; i < jml; i++){
                    $("#detail_wakil").append('<div class="input-group wakil"><a  class="btn default" >'+(i + 1)+'. </a><input name="wakil_nama" class="form-control input-small " /><a  class="btn default" >HP / No Telp</a><input name="wakil_telpon" class="form-control input-small" /></div>');
                }
            }else{
                for(i = current; i > jml; i--){
                    $("#detail_wakil").find(".wakil").get(i - 1).remove();
                }
            }
            
            
        });
	});
</script>
	<div class="portlet box blue" hidden=hidden tabindex="-1" id="portlet-mandor">
	   <div class="portlet-title">
		  <div class="caption"><i class="icon-reorder"></i>Form mandor</div>
		  <div class="tools">
			 <a href="javascript:;" class="collapse"></a>
		  </div>
	   </div>

		<div class="portlet-body form">
		<!-- BEGIN FORM-->
		<form action="#" id="form-mandor" class="horizontal-form" onSubmit="javascript:return false;">

		<input type="hidden" value="INSERT" name="task" id="task-mandor">
		<input type="hidden"  id="mandor_id" name="mandor_id" />
		
		<input id="task_id" name="task_id" hidden=hidden  />

			 <div class="form-body">
				<h3 class="form-section">Informasi Mandor</h3>

				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">Nama Mandor:</label><input id="mandor_nama" name="mandor_nama" class="form-control" /></div></div>
                    <div class="col-md-6"><div class="form-group"><label class="control-label">Kode:</label><input id="mandor_kode" name="mandor_kode" class="form-control" /></div></div></div>
                 
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">Status: </label><br /><input type="radio" name="mandor_status"   value="Badan Hukum" checked /> Badan Hukum <input  type="radio" name="mandor_status" value="Perorangan" /> Perorangan</div></div>
                    <div class="col-md-6"><div class="form-group"><label class="control-label">NPWP:</label><input id="mandor_npwp" name="mandor_npwp" class="form-control" /></div></div></div>
                    
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">Badan Usaha:</label><input id="mandor_bu" name="mandor_bu" class="form-control" /></div></div> </div>
                 
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">Alamat:</label><textarea id="mandor_alamat" name="mandor_alamat" class="form-control" ></textarea></div></div>
                 <div class="col-md-6"><div class="form-group"><label class="control-label">Kota:</label><input id="mandor_kota" name="mandor_kota" class="form-control" /></div></div>
                 </div>
                 
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">No Telp:</label><input id="mandor_telpon" name="mandor_telpon" class="form-control" /></div></div>
                    <div class="col-md-6"><div class="form-group"><label class="control-label">Fax:</label><input id="mandor_fax" name="mandor_fax" class="form-control" /></div></div></div>
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">Email:</label><input id="mandor_email" name="mandor_email" class="form-control" /></div></div></div>
                 
				<h3 class="form-section">Kemampuan Dan Pengalaman</h3>
                 
				<div class="row"><div class="col-md-4"><div class="form-group"><label class="control-label">Pengalaman Di PT. PP:</label><div class="input-group"><input id="mandor_pengalaman" name="mandor_pengalaman" class="form-control input-small" />
                                    <a class="btn default" >Tahun</a>
                                    </div> </div></div>
                    <div class="col-md-8"><div class="form-group"><label class="control-label">Kemampuan:</label><select id="mandor_kemampuan" name="mandor_kemampuan" class="form-control"  multiple></select></div></div></div>
                 
				<h3 class="form-section">Kemampuan Sumber Daya Manusia</h3>
                 
				<div class="row"><div class="col-md-4"><div class="form-group"><label class="control-label">Jumlah Wakil:</label>
                    <div class="input-group"><input id="mandor_wakil" name="mandor_wakil" class="form-control input-small" /><a  class="btn default" >Orang</a>
                                    </div></div></div>
                 <div class="col-md-8">
                     
                    <div class="form-group"><label class="control-label">Detail Wakil:</label>
                    
                        <div id="detail_wakil">
                        </div>
                     </div>
                    
                    </div>
                 </div>
            
				<h3 class="form-section">Jumlah Tenaga Yang Bisa Disupply</h3>
                 
				<div class="row"><div class="col-md-4"><div class="form-group"><label class="control-label">Tukang Kelas 1:</label><div class="input-group"><input id="mandor_tukang1" name="mandor_tukang1" class="form-control input-small" /><a  class="btn default" >Orang</a></div></div></div>
                    <div class="col-md-4"><div class="form-group"><label class="control-label">Tukang Kelas 2 / Wakil:</label><div class="input-group"><input id="mandor_tukang2" name="mandor_tukang2" class="form-control input-small" /><a  class="btn default" >Orang</a></div></div></div>
                    <div class="col-md-4"><div class="form-group"><label class="control-label">Pekerja:</label><div class="input-group"><input id="mandor_pekerja" name="mandor_pekerja" class="form-control input-small" /><a  class="btn default" >Orang</a></div></div></div></div>
                 
				<h3 class="form-section">Keuangan Dan Pendanaan</h3>
                 
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">Kemampuan Keuangan / Financial:</label><select id="m_keuangan_id" name="m_keuangan_id" class="form-control" ></select></div></div>
                    <div class="col-md-6"><div class="form-group"><label class="control-label">Sumber Pendanaan Saat Ini:</label><select id="m_pendanaan_id" name="m_pendanaan_id" class="form-control" ></select></div></div></div>
		
			 </div>
			 <div class="form-actions right">
				<button type="reset" id="btn-cancel-mandor" class="btn default">Cancel</button>
				<button type="submit" id="btn-save-mandor" class="btn blue"><i class="icon-ok"></i> Save</button>
			 </div>
		  </form>
		  <!-- END FORM--> 
	   </div>
	</div>
    
	 <div class="row">
		<div class="col-md-12">
		   <!-- BEGIN SAMPLE TABLE PORTLET-->
		   <div class="portlet box yellow">
			  <div class="portlet-title">
				 <div class="caption"><i class="icon-cogs"></i>mandor</div>
				 <div class="tools">
					<a href="javascript:;" class="collapse"></a>
				 </div>
			  </div>
			  <div class="portlet-body flip-scroll">
			  
			<button id="btn-add-mandor" class="btn btn-primary">Add</button>
			<p>&nbsp;</p>
			
                      <div class="input-group search">
                         <input type="text" class="form-control" placeholder="Filter">
                         <span class="input-group-btn">
                         <button type="button" class="btn yellow">Search !</button>
                         </span>
                      </div>
			<p>&nbsp;</p>
			  <div id="grid-mandor" class="table-responsive"></div>
				</div>
			 </div>
		</div>
	</div>